from manim import *


class math(Scene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(BLUE).set_opacity(0.5)
                line1.set_stroke(width=10)
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(BLUE).set_opacity(0.5)
                line2.set_stroke(width=10)
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])   
        

        #######################################################################################################
        #设置题目并写出
        text = Tex(r"\text{已知函数} $f\left( x\right) =x^{2}ln\left( 2-x\right)  $"+
                   r"\text{，则当}$n\geq 3$"+
                   r"\text{时，}$f^{\left( n\right) }\left( 0\right) =$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
        self.play(Write(text),FadeIn(dot1),FadeIn(dot2),FadeIn(dot3))

        #题目向上移动

        self.play(text.animate().scale(0.8).move_to(UP*20)) 

        #分析文本
        text0=MarkupText("<b><i>分析：</i></b>",color=PINK).scale(5.0).move_to([-35,7.5,0])
        self.play(Create(text0))

        #中心主题+框框标记
        text1 = Tex(r"\text{高阶导数的计算}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,0,0])
        self.play(Write(text1))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=23,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-20, 0, 0])
        self.play(Create(rectangle))
        #箭头引出
        arrow = Arrow(start=[-8,2.5,0],end=[0,10,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本2
        text2 = Tex(r"\text{找规律}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7.5,10,0])
        self.play(Write(text2))

        #箭头1引出
        arrow1 = Arrow(start=[-8,1,0],end=[0,5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow1))
        #文本3
        text3 = Tex(r"\text{奇偶性}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7.5,5,0])
        self.play(Write(text3))

        #箭头2引出
        arrow2 = Arrow(start=[-8,0,0],end=[0,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow2))
        #文本4
        text4 = Tex(r"\text{莱布尼兹}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7.5,0,0])
        self.play(Write(text4))

        #箭头3引出
        arrow3 = Arrow(start=[-8,-1,0],end=[0,-5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow3))
        #文本5
        text5= Tex(r"\text{泰勒展开}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7.5,-5,0])
        self.play(Write(text5))

        #箭头4引出
        arrow4 = Arrow(start=[-8,-2,0],end=[0,-10,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow4)) 
        #文本6
        text6= Tex(r"\text{构造递推}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([7.5,-10,0])
        self.play(Write(text6))
        #框框标记+箭头引出+文本
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=4
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle1.move_to([7.5,10,0])
        self.play(Create(rectangle1))
        arrow5 = Arrow(start=[15,10,0],end=[20,10,0], color=BLUE
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow5)) 
        text7= Tex(r"$f^{\left( n\right) }\left( x\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,10,0])
        self.play(Write(text7))
        #框框标记+箭头引出+文本
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=19
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle2.move_to([7.5,-2.5,0])
        self.play(Create(rectangle2))
        arrow6 = Arrow(start=[15,-2.5,0],end=[20,-2.5,0], color=BLUE
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow6)) 
        text8= Tex(r"$f^{\left( n\right) }\left( x_{0}\right)  $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-2.5,0])
        self.play(Write(text8))
        #框框快速闪动
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=19
                            ,stroke_color=PURPLE,stroke_width=20)
        rectangle3.move_to([7.5,-2.5,0])
        self.play(Create(rectangle3))
        #淡出解析内容
        self.play(FadeOut(text0,text1,text2,text3,text4,text5,text6,text7,text8,arrow,arrow1,arrow2,arrow3,arrow4,arrow5,arrow6,rectangle,rectangle1,rectangle2,rectangle3))
        
        #分析文本
        text9=MarkupText("<b><i>解：</i></b>",color=PINK).scale(5.0).move_to([-35,7.5,0])
        self.play(Create(text9))

        #框框标记
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=7,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle3.move_to([20,20,0])
        self.play(Create(rectangle3))
        #半透明矩阵扩展
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-17.5,20,0])
        self.play(
            rectangle4.animate.shift(10*RIGHT).stretch(19.0,dim=0)
        )
        #泰勒展开文本
        text9= Tex(r"$ln\left( 1+x\right) =x-\dfrac{x^{2}}{2}+\dfrac{x^{3}}{3}+\ldots +\dfrac{\left( -1\right) ^{n-1}x^{n}}{n}+o\left( x^{n}\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,7.5,0])
        self.play(Write(text9))
        #框框消失
        self.play(FadeOut(rectangle3))
        #泰勒展开文本
        text10= Tex(r"$\ln \left( 2-x\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-5,0])
        self.play(Write(text10))
        #框框标记
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=3,height=5
                            ,stroke_color=YELLOW,stroke_width=10)
        rectangle5.move_to([-26,7.5,0])
        rectangle6=Rectangle(color=BLUE,fill_opacity=0.0,width=3,height=5
                            ,stroke_color=YELLOW,stroke_width=10)
        rectangle6.move_to([-26,-5,0])
        self.play(Create(rectangle5),Create(rectangle6))  
        #泰勒展开文本
        text11= Tex(r"$=ln\left[ 2\left( 1-\dfrac{x}{2}\right) \right] =\ln 2+\ln \left( 1-\dfrac{x}{2}\right)  $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text10,RIGHT,buff=0.5)
        self.play(Write(text11)) 
        #框框移动
        self.play(rectangle6.animate().shift(41*RIGHT))
        
        #框框再次移动并扩大
        self.play(rectangle5.animate().shift(4*RIGHT).stretch(1.5,dim=0),rectangle6.animate().shift(4*RIGHT).stretch(1.5,dim=0))
        ##框框消失
        self.play(FadeOut(rectangle5,rectangle6))
        #文本
        text12= Tex(r"$=\ln 2+\left( -\dfrac{x}{2}\right) -\dfrac{\left( -\dfrac{x}{2}\right) ^{2}}{2}+\dfrac{\left( -\dfrac{x}{2}\right) ^{3}}{3}+\ldots +\dfrac{\left( -\dfrac{x}{2}\right) ^{n}}{n}+o\left( x^{n}\right)$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-15,0])
        self.play(Write(text12))
        #屏幕清除
        self.play(FadeOut(rectangle4,text9,text10,text11,text12))
        #泰勒展开文本
        text13= Tex(r"$f\left( x\right) =x^{2}\ln \left( 2-x\right) = $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,10,0])
        self.play(Write(text13)) 
        text14= Tex(r"$x^{2}\ln 2+x^{2}\cdot \left( \dfrac{-x}{2}\right) +\ldots +\dfrac{\left( -1\right) ^{n-3}x^{n}}{n-2}+\ldots +\dfrac{\left( -1\right) ^{n-1}x^{n+2}}{n}+o\left( x^{n+2}\right)  $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,3,0])
        self.play(Write(text14)) 
        text15= Tex(r"$f\left( x\right) =f\left( 0\right) +f'\left( 0\right) x+\dfrac{f''\left( 0\right) }{2!}x^{2}+\ldots +\dfrac{f^{\left( n\right) }\left( 0\right) }{n!}x^{n}+o\left( x\right) ^{n}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(Write(text15)) 
        #框框标记
        rectangle7=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=8
                            ,stroke_color=YELLOW,stroke_width=10)
        rectangle7.move_to([-2.5,2.5,0])
        rectangle8=Rectangle(color=BLUE,fill_opacity=0.0,width=13,height=8
                            ,stroke_color=YELLOW,stroke_width=10)
        rectangle8.move_to([17.5,-5,0])
        self.play(Create(rectangle7),Create(rectangle8))  
        #清空屏幕
        self.play(FadeOut(text13,text14,text15,rectangle7,rectangle8))                       
        
